<?php

require_once dirname(BASE_PATH) . '/custom/custom.php';

class hangzhouController extends Custom{

	function display($type,$condition){
		if($type == 'goods'){
			$json = array('Commoditys' => array());
			$count = 0;
			foreach($condition as $c => $val){
				$count++;
				$str = (string)$count;
				while(strlen($str) < 4){
					$str  = '0' . $str;
				}
				$con = array('goods_commonid' => $val);
				$data = array(
					'goods' => Model()->table('goods')->where($con)->find(),
					'goods_common' => Model()->table('goods_common')->where($con)->find(),
					'goods_extend' => Model()->table('custom_hangzhou_goods')->where($con)->find()
				);
				$json['Commoditys'][] = array(
					"commodityCode" => $this->config['config']['company_code'] . $str,
					"commodityName" => $data['goods']['goods_name'],
					'commoditySpec' => isset($data['goods_extend']['commoditySpec']) ? $data['goods_extend']['commoditySpec'] : '',
					'commodityArtNo' => isset($data['goods_extend']['commodityArtNo']) ? $data['goods_extend']['commodityArtNo'] : '',
					'commodityMadeTime' => isset($data['goods_extend']['commodityMadeTime']) ? $data['goods_extend']['commodityMadeTime'] : '',
					'durabilityPeriod' => isset($data['goods_extend']['durabilityPeriod']) ? $data['goods_extend']['durabilityPeriod'] : '',
					'brandId' => isset($data['goods_extend']['brandId']) ? $data['goods_extend']['brandId'] : '',
					'price' => isset($data['goods_common']['goods_costprice']) ? $data['goods_common']['goods_costprice'] : '',
					'marketPrice' => isset($data['goods_common']['goods_marketprice']) ? $data['goods_common']['goods_marketprice'] : '',
					'serialcontrol ' => isset($data['goods_extend']['serialcontrol ']) ? $data['goods_extend']['serialcontrol '] : '',
					'typeId' => isset($data['goods_extend']['typeId']) ? $data['goods_extend']['typeId'] : '',
					'unit' => isset($data['goods_extend']['unit']) ? $data['goods_extend']['unit'] : '',
					'weight' => isset($data['goods_extend']['weight']) ? $data['goods_extend']['weight'] : '',
					'boxSpec' => isset($data['goods_extend']['boxSpec']) ? $data['goods_extend']['boxSpec'] : '',
					'remark' => isset($data['goods_extend']['remark']) ? $data['goods_extend']['remark'] : '',
					'madeAddr' => isset($data['goods_extend']['madeAddr']) ? $data['goods_extend']['madeAddr'] : '',
					'tradeCountryName' => isset($data['goods_extend']['tradeCountryName']) ? $data['goods_extend']['tradeCountryName'] : '',
					'tradeCountryCode ' => isset($data['goods_extend']['tradeCountryCode ']) ? $data['goods_extend']['tradeCountryCode '] : '',
					'HScode' => isset($data['goods_extend']['HScode']) ? $data['goods_extend']['HScode'] : '',
				);
			}
		}

		$str = '原始数据为';
		$str .= print_r($json,true);
		$str .= PHP_EOL;
		$str .= PHP_EOL;
		$str .= PHP_EOL;
		$str .= PHP_EOL;
		$param = array();
		$param['v'] = $this->config['config']['v'];
		$param['ip'] = $this->config['config']['ip'];
		$param['sessionKey'] = $this->config['config']['sessionKey'];
		$param['datetime'] = date("YmdHis");
		$sign = $param['v'] . $param['ip'] . $param['sessionKey'] . $param['datetime'] . 'SPWLA';
		$sign = strtoupper(md5($sign));
		$param['sign'] = $sign;
		$json = json_encode($json);
		$param['JSON_OBJ'] = base64_encode($json);

		$str .= "生成报文为" . print_r($param,true);

		$str .= PHP_EOL;
		$str .= PHP_EOL;
		$str .= PHP_EOL;
		$str .= PHP_EOL;


		$url = 'http://60.191.39.196:8001/ecm/interface/rest/SPWLInterface/sendCommodity';

		$ret = post_get($url,$param);

		$str .= $ret;

		$filename = dirname(BASE_PATH) . '/custom/hangzhou/' . time() . '.txt';

		file_put_contents($filename,$str);

		echo '请求成功，结果保存为' . PHP_EOL . $filename;
	}
}

function file_get_contents_post($url, $post) {  
    $options = array(  
        'http' => array(  
            'method' => 'POST',  
            // 'content' => 'name=caiknife&email=caiknife@gmail.com',  
            'content' => http_build_query($post),  
        ),  
    );  
  
    $result = file_get_contents($url, false, stream_context_create($options));  
  
    return $result;  
}  

function post_get($url,$arr){
	$post_data = array();
	foreach($arr as $key => $val){
		$post_data[] = "$key=$val";
	}
    $post_data = implode('&',$post_data);  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_POST, 1);  
    curl_setopt($ch, CURLOPT_URL,$url);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
    ob_start();  
    curl_exec($ch);  
    $result = ob_get_contents() ;  
    ob_end_clean(); 
    return $result;
}